using System;

namespace ArtheaEngine
{
    partial class Social
    {
        public static bool Interpret(Character ch, Argument argument)
        {
            string arg = argument.Peek();

            foreach (Social s in World.DataContext.Socials)
            {
                if (!s.Name.StartsWith(arg, StringComparison.CurrentCultureIgnoreCase)) continue;

                argument.FirstArg();
                s.Parse(ch, argument);
                return true;
            }
            return false;
        }

        public static Social FindByName(string name)
        {
            foreach (Social s in World.DataContext.Socials)
            {
                if (!s.Name.StartsWith(name, StringComparison.CurrentCultureIgnoreCase)) continue;

                return s;
            }

            return null;
        }

        public void Parse(Character ch, Argument argument)
        {
            if (argument.IsEmpty())
            {
                ch.Act(ActType.ROOM, OthersNoArg);
                ch.Act(ActType.CHAR, CharNoArg);
                return;
            }

            Character victim = ch.Room.Characters.FindByName(argument.ToString());

            if (victim == null)
            {
                Object obj = ch.Room.Objects.FindByName(argument);

                if (obj == null)
                {
                    ch.WriteLine("That person or object is not here.");
                    return;
                }

                ch.Act(ActType.CHAR, CharObjFound, obj);
                ch.Act(ActType.ROOM, OthersObjFound, obj);
                return;
            }

            if (victim == ch)
            {
                ch.Act(ActType.CHAR, CharSelf);
                ch.Act(ActType.ROOM, OthersSelf);
                return;
            }

            ch.Act(ActType.CHAR, CharFound, victim);
            victim.Act(ActType.CHAR, VictimFound, ch);
            victim.Act(ActType.NOTCHAR, OthersFound, ch);
            return;
        }
    }
}